package util;

import java.util.Deque;
import java.util.LinkedList;

public class TreeUtil {
    private final static char SEP = ',';
    private final static String NULL = "null";

    public static String serialize(TreeNode root) {
        if(root == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        Deque<TreeNode> dq = new LinkedList<>();
        dq.addLast(root);
        while(!dq.isEmpty()) {
            TreeNode front = dq.removeFirst();
            if(front != null) {
                sb.append(String.valueOf(front.val)).append(SEP);
                dq.addLast(front.left);
                dq.addLast(front.right);
            } else {
                sb.append(NULL).append(SEP);
            }
        }
        sb.append("]");
        return sb.toString();
    }
    public static TreeNode deserialize(String data) {
        if(data.equals("") || data.equals("[]")) return null;
        String[] dataList = data.substring(1, data.length()-1).split(String.valueOf(SEP));
        TreeNode root = new TreeNode(Integer.parseInt(dataList[0]));
        Deque<TreeNode> dq = new LinkedList<>();
        dq.addLast(root);
        int pos = 1;
        while(!dq.isEmpty()) {
            TreeNode front = dq.removeFirst();
            if(pos < dataList.length && !String.valueOf(NULL).equals(dataList[pos])) {
                front.left = new TreeNode(Integer.parseInt(dataList[pos]));
                dq.addLast(front.left);
            }
            ++pos;
            if(pos < dataList.length && !String.valueOf(NULL).equals(dataList[pos])) {
                front.right = new TreeNode(Integer.parseInt(dataList[pos]));
                dq.addLast(front.right);
            }
            ++pos;
        }
        return root;
    }
    private static void preorder(TreeNode root) {
        if(root == null) {
            System.out.print("null ");
            return;
        }
        System.out.print(root.val + " ");
        if(root.left != null || root.right != null) {
            preorder(root.left);
            preorder(root.right);
        }
    }
    public static void printPreorder(TreeNode root) {
        if(root == null) {
            System.out.println("The tree is empty");
        } else {
            System.out.print("PreOrder: ");
            preorder(root);
            System.out.print("\n");
        }
    }
    public static void printLevel(TreeNode root) {
        if(root == null) {
            System.out.println("The tree is empty");
            return;
        }
        System.out.print("Level: ");
        Deque<TreeNode> dq = new LinkedList<>();
        dq.addLast(root);
        while(!dq.isEmpty()) {
            TreeNode front = dq.removeFirst();
            System.out.print(front == null ? "null" : front.val);
            System.out.print(" ");
            if(front != null) {
                if(front.left != null || front.right != null) {
                    dq.addLast(front.left);
                    dq.addLast(front.right);
                }
            }
        }
        System.out.print("\n");
    }
}
